/****************************************************************************
  Copyright (C) 2010 Wanderson Regis Silva
  All rights reserved
  Contact: Wanderson Regis Silva (wanderson.regis@ymail.com)

  This file is part of OpenEditor project

  This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

#ifndef CODEEDIT_H
#define CODEEDIT_H

#include <qplaintextedit.h>
#include <qtextobject.h>

class LineNumberArea;

class CodeEdit : public QPlainTextEdit {
  LineNumberArea* lines;
  QString __fileName;

  Q_OBJECT
public:
  explicit CodeEdit(QWidget *parent = 0) : QPlainTextEdit(parent) { init(); };
  explicit CodeEdit(const QString &text, QWidget *parent = 0) : QPlainTextEdit(text, parent) { init(); };

  QString fileName(void) { return __fileName; };
  void lineNumberAreaPaintEvent(QPaintEvent *event);
  int lineNumberAreaWidth(void) const;
  bool loadFile(const QString &fileName = QString());
  inline void revert(void) { loadFile(); };
  bool saveFile(const QString &fileName = QString());
  void setFileName(const QString &fileName) { __fileName = fileName; };
  inline void setText(const QString &text) { setPlainText(text); };
  void zoomIn(const int &range = 1);
  void zoomOut(const int &range = 1);
protected:
  void closeEvent(QCloseEvent *event);
  void resizeEvent(QResizeEvent *event);
private slots:
  void highlightCurrentLine(void);
  void updateLineNumberAreaWidth(const int &newBlockCount);
  void updateLineNumberArea(const QRect &rect, int dy);
private:
  void init();
};

class LineNumberArea : public QWidget {
  Q_OBJECT
public:
  explicit LineNumberArea(QWidget *parent = 0) : QWidget(parent) { };
  const QSize sizeHint(void) {
    return QSize(((CodeEdit*)parent())->lineNumberAreaWidth(), 0);
  };
protected:
  void paintEvent(QPaintEvent *event) {
    ((CodeEdit*)parent())->lineNumberAreaPaintEvent(event);
  };
};

#endif // CODEEDIT_H
